*The data (wave 7 and 8 of the British Election Study) can be downloaded at https://www.britishelectionstudy.com/data-objects/panel-study-data/page/4/

*Merging data waves (W7 and W8)
use Wave7
sort id time
append using Wave8
sort id time
bro id time


*Generating variable indicating whether respondents participated in both survey waves
duplicates report id 
bysort id : gen dropped_out=1 if _N < 2
duplicates report id 
sort id time
bro id time dropped_out

*Giving value labels to time indicator
codebook time 
recode time (1=0 "Wave 7") (2=1 "Wave 8"), gen(timedummy)
codebook time timedummy
drop time 


******Coding dependent variable (inclination to vote remain/ policy opinion)********
codebook euRefVote
recode euRefVote (0=1 "Vote Remain") (1 2 9999=0 "Vote Leave (+ don't know or did not vote)"), gen(vote_remain)
codebook euRefVote vote_remain


*******Constructing independent variable (learning policy info)
codebook effectsEUTrade
recode effectsEUTrade (1 2=1 "Correct perception") (3 4 5 9999=0 "Incorrect perception"), gen(trade_perception)
codebook effectsEUTrade trade_perception


gen learn_policyinfo=.
sort id timedummy
by id (timedummy), sort: replace learn_policyinfo=0 if trade_perception[_n-1]==0 & (trade_perception==0)
by id (timedummy), sort: replace learn_policyinfo=1 if trade_perception[_n-1]==0 & (trade_perception==1)
by id (timedummy), sort: replace learn_policyinfo=2 if trade_perception[_n-1]==1 & (trade_perception==1)
by id (timedummy), sort: replace learn_policyinfo=3 if trade_perception[_n-1]==1 & (trade_perception==0)
label define learn_policyinfo 0 "Never Learned" 1 "Learned Policy Info" 2 "Already Knew" 3 "Forgot"
label values learn_policyinfo learn_policyinfo
codebook learn_policyinfo

bro id timedummy learn_policyinfo trade_perception

*Adding values to misssing observation regarding independent variable (learning)
bysort id (timedummy) : replace learn_policyinfo = learn_policyinfo[_n+1] if missing(learn_policyinfo)

*Checking coding was made correct
bro id timedummy trade_perception learn_policyinfo effectsEUTrade effectsEUTrade
tab timedummy learn_policyinfo, row





************************Control Variables************************



*******Time-invariant background variables - used for matching and control for unispecific time-trends********

*Gender
codebook gender
gen gender1=gender if timedummy==0
bysort id (timedummy) : replace gender1 = gender1[_n-1] if missing(gender1)
codebook gender gender1 if timedummy==0
label define gender1 1 "Male" 2 "Female"
label values gender1 gender1
codebook gender1
drop gender
rename gender1 gender
codebook gender
tab timedummy gender if dropped_out!=1, row

*Age
codebook age, tab(99)
gen age_cat=. if timedummy==0
replace age_cat=1 if inrange(age, 17, 29.1) & timedummy==0
replace age_cat=2 if inrange(age, 29.9, 39.1) & timedummy==0
replace age_cat=3 if inrange(age, 39.9, 49.1) & timedummy==0
replace age_cat=4 if inrange(age, 49.9, 59.1) & timedummy==0
replace age_cat=5 if inrange(age, 59.9, 69.1) & timedummy==0
replace age_cat=6 if inrange(age, 69.9, 120) & timedummy==0
bysort id (timedummy) : replace age_cat = age_cat[_n-1] if missing(age_cat)
label define age_cat 1 "18-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60-69" 6 "70+"
label values age_cat age_cat
codebook age_cat
tab timedummy age_cat if dropped_out!=1, row

*Education
codebook p_edlevelUni
gen education1=p_edlevelUni if timedummy==0
bysort id (timedummy) : replace education1 = education1[_n-1] if missing(education1)
codebook p_edlevelUni education1 if timedummy==0
label define education1 0 "No qualifications" 1 "Below GCSE" 2 "GCSE" 3 "A-level" 4 "Undergraduate" 5 "Postgrad"
label values education1 education1
codebook education1
drop education
rename education1 education
codebook education
tab timedummy education if dropped_out!=1, row

*Occupation
codebook p_work_stat
gen occupation=p_work_stat if timedummy==0
bysort id (timedummy) : replace occupation = occupation[_n-1] if missing(occupation)
codebook p_work_stat occupation if timedummy==0
label define occupation 1 "Working full time (30 hours or more per week)" 2 "Working part time (8-29 hours per week)" 3 "Working part time (8 hours or less per week)" 4 "Full time student" 5 "Retired" 6 "Unemployed" 7 "Not working" 8 "Other"
label values occupation occupation
codebook occupation
tab timedummy occupation if dropped_out!=1, row

*Region of residence
codebook gor, tab(99)
gen region=gor if timedummy==0
bysort id (timedummy) : replace region = region[_n-1] if missing(region)
codebook gor region if timedummy==0, tab(99)
label define region 1 "North East" 2 "North West" 3 "Yorkshire and the Humber" 4 "East Midlands" 5 "West Midlands" 6 "East of England" 7 "London" 8 "South East" 9 "South West" 10 "Wales" 11 "Scotland"
label values region region
codebook region, tab(99)
tab timedummy region if dropped_out!=1, row

*Partisanship - measured in first wave (used for analysis where I restrict the pool to only include voters who support a party recommending a "remain-vote")
codebook partyId partyIdSqueeze, tab(99)
gen prior_partisanship=0 if timedummy==0
replace prior_partisanship=1 if timedummy==0 & (partyId==6 | partyIdSqueeze==6)
replace prior_partisanship=2 if timedummy==0 & (partyId==1 | partyIdSqueeze==1)
replace prior_partisanship=3 if timedummy==0 & (partyId==2 | partyIdSqueeze==2)
replace prior_partisanship=3 if timedummy==0 & (partyId==3 | partyIdSqueeze==3)
replace prior_partisanship=3 if timedummy==0 & (partyId==4 | partyIdSqueeze==4)
replace prior_partisanship=3 if timedummy==0 & (partyId==5 | partyIdSqueeze==5)
replace prior_partisanship=3 if timedummy==0 & (partyId==7 | partyIdSqueeze==7)
codebook partyId partyIdSqueeze  prior_partisanship if timedummy==0, tab(99)

bysort id (timedummy) : replace prior_partisanship = prior_partisanship[_n-1] if missing(prior_partisanship)


label define prior_partisanship 0 "No party affilliation" 1 "Supports party recommending BREXIT (UKIP)" 2 "Supports party in division on BREXIT (Conservatives)" 3 "Supports party recommending REMAIN (Labour, LibDem, Greens, SNP, PC)"
label values prior_partisanship prior_partisanship
codebook prior_partisanship, tab(99)
tab timedummy prior_partisanship if dropped_out!=1, row

*Prior ideological self-placement (used for matching)
codebook leftRight, tab(99)
gen prior_ideology=leftRight if timedummy==0
bysort id (timedummy) : replace prior_ideology = prior_ideology[_n-1] if missing(prior_ideology)
codebook leftRight prior_ideology if timedummy==0, tab(99)
replace prior_ideology=5 if prior_ideology==9999
label define ideology 0 "Left" 10 "Right"
label values prior_ideology ideology
codebook prior_ideology, tab(99)
tab timedummy prior_ideology if dropped_out!=1, row 


*Prior EU values - opinion in EU integration (used for matching)
codebook EUIntegrationSelf, tab(99)
gen prior_EU_values=EUIntegrationSelf if timedummy==0
bysort id (timedummy) : replace prior_EU_values = prior_EU_values[_n-1] if missing(prior_EU_values)
codebook EUIntegrationSelf prior_EU_values if timedummy==0, tab(99)
recode prior_EU_values (0=0 "Unite fully with the European Union") (1=1) (2=2) (3=3) (4=4) (5 9999=5) (6=6) (7=7) (8=8) (9=9) (10=10 "Protect our independence") , gen(prior_EU_values1)
codebook prior_EU_values1 EUIntegrationSelf if timedummy==0
drop prior_EU_values
rename prior_EU_values1 prior_EU_values
tab timedummy prior_EU_values if dropped_out!=1, row

*Party like/dislike ranking (used for matching)
codebook likeCon likeLab likeLD likeUKIP likeGrn, tab(99)
recode likeCon (9999=5), gen(party_like_Con)
recode likeLab (9999=5), gen(party_like_Lab)
recode likeLD (9999=5), gen(party_like_LD)
recode likeUKIP (9999=5), gen(party_like_UKIP)
recode likeGrn (9999=5), gen(party_like_Grn)
codebook party_like_Con party_like_Lab party_like_LD party_like_UKIP party_like_Grn, tab(99)


*********Time-varying control variables********


*Perceived government performance (satisfaction with democracy)
codebook satDemUK
recode satDemUK (1=1 "Very dissatisfied") (2=2 "A little dissatisfied") (3=3 "Fairly satisfied") (4=4 "Very satisfied") (9999=.), gen(democraticsatisfaction)
codebook satDemUK democraticsatisfaction


*Interest in politics regarding the EU
codebook euRefInterest 
recode euRefInterest (4=4 "Very interested") (3=3 "Somewhat interested") (2=2 "Not very interested") (1=1 "Not at all interested") (9999=.), gen(EU_interest)
codebook euRefInterest EU_interest

*Exposure to referendum debate (index construction)
codebook euSources_1 euSources_2 euSources_3 euSources_4 euSources_5

recode euSources_1 (1=1 "Yes (have heard about EU referendum through television within seven days)") (0 9999=0 "No (+ don't know)"), gen(exposure1)
codebook euSources_1 exposure1

recode euSources_2 (1=1 "Yes (have heard about EU referendum through newspapers within seven days)") (0 9999=0 "No (+ don't know)"), gen(exposure2)
codebook euSources_2 exposure2

recode euSources_3 (1=1 "Yes (have heard about EU referendum through radio within seven days)") (0 9999=0 "No (+ don't know)"), gen(exposure3)
codebook euSources_3 exposure3

recode euSources_4 (1=1 "Yes (have heard about EU referendum through the internet within seven days)") (0 9999=0 "No (+ don't know)"), gen(exposure4)
codebook euSources_4 exposure4

recode euSources_5 (1=1 "Yes (have heard about EU referendum through talking to other people within seven days)") (0 9999=0 "No (+ don't know)"), gen(exposure5)
codebook euSources_5 exposure5

pwcorr exposure1 exposure2 exposure3 exposure4 exposure5
alpha exposure1 exposure2 exposure3 exposure4 exposure5, item min(5)
alpha exposure1 exposure2 exposure3 exposure4 exposure5, gen(exposure_index) min(5)
hist exposure_index
sum exposure_index
codebook exposure_index, tab(99)
recode exposure_index (0=0 "0 sources of debate exposure") (0.2=1 "1 sources of debate exposure") (0.40000001=2 "2 sources of debate exposure") (0.60000002=3 "3 sources of debate exposure") (0.80000001=4 "4 sources of debate exposure") (1=5 "5 sources of debate exposure"), gen(debateexposure)
codebook exposure_index debateexposure, tab(99)
drop exposure_index



****Learning your party's position (vote recommendation)


*Parti-ID
codebook partyId partyIdSqueeze if timedummy==0, tab(99)

gen parti_id=. 
replace parti_id=1 if (partyId==1 | partyIdSqueeze==1) & timedummy==0
replace parti_id=2 if (partyId==2 | partyIdSqueeze==2) & timedummy==0
replace parti_id=3 if (partyId==3 | partyIdSqueeze==3) & timedummy==0
replace parti_id=4 if (partyId==4 | partyIdSqueeze==4) & timedummy==0
replace parti_id=5 if (partyId==5 | partyIdSqueeze==5) & timedummy==0

bysort id (timedummy) : replace parti_id = parti_id[_n-1] if missing(parti_id)


label define parti_id 1 "Conservative" 2 "Labour" 3 "Liberal Democrat" 4 "Scottish National Party (SNP)" 5 "Plaid Cymru"
label values parti_id parti_id
codebook parti_id	


*Knowledge of party's positions on referendum issue
codebook labSupport conSupport ldSupport snpSupport plaidSupport

recode labSupport (1=1 "Know the correct party position") (2 3 9999=0 "Don't know the correct party position"), gen(partyposition_Labour)
codebook labSupport partyposition_Labour

recode conSupport (2=1 "Know the correct party position") (1 3 9999=0 "Don't know the correct party position"), gen(partyposition_Conservative)
codebook conSupport partyposition_Conservative

recode ldSupport (1=1 "Know the correct party position") (2 3 9999=0 "Don't know the correct party position"), gen(partyposition_LibDem)
codebook ldSupport partyposition_LibDem

recode snpSupport (1=1 "Know the correct party position") (2 3 9999=0 "Don't know the correct party position"), gen(partyposition_SNP)
codebook snpSupport partyposition_SNP

recode plaidSupport (1=1 "Know the correct party position") (2 3 9999=0 "Don't know the correct party position"), gen(partyposition_Plaid)
codebook plaidSupport partyposition_Plaid

codebook partyposition_Labour partyposition_Conservative partyposition_LibDem partyposition_SNP partyposition_Plaid

*Matching party supporters with knowledge of their positions

gen know_own_partyposition=.

replace know_own_partyposition=0 if parti_id==1 & partyposition_Conservative==0
replace know_own_partyposition=0 if parti_id==2 & partyposition_Labour==0
replace know_own_partyposition=0 if parti_id==3 & partyposition_LibDem==0
replace know_own_partyposition=0 if parti_id==4 & partyposition_SNP==0
replace know_own_partyposition=0 if parti_id==5 & partyposition_Plaid==0


replace know_own_partyposition=1 if parti_id==1 & partyposition_Conservative==1
replace know_own_partyposition=1 if parti_id==2 & partyposition_Labour==1
replace know_own_partyposition=1 if parti_id==3 & partyposition_LibDem==1
replace know_own_partyposition=1 if parti_id==4 & partyposition_SNP==1
replace know_own_partyposition=1 if parti_id==5 & partyposition_Plaid==1


codebook know_own_partyposition
label define know_own_partyposition 0 "Don't know owns party's recommendation" 1 "Knows owns party's recommendation"
label values know_own_partyposition know_own_partyposition
codebook know_own_partyposition

gen learn_partyposition=.
sort id timedummy
by id (timedummy), sort: replace learn_partyposition=0 if know_own_partyposition[_n-1]==0 & (know_own_partyposition==0)
by id (timedummy), sort: replace learn_partyposition=1 if know_own_partyposition[_n-1]==0 & (know_own_partyposition==1)
by id (timedummy), sort: replace learn_partyposition=2 if know_own_partyposition[_n-1]==1 & (know_own_partyposition==1)
by id (timedummy), sort: replace learn_partyposition=3 if know_own_partyposition[_n-1]==1 & (know_own_partyposition==0)
label define learn_partyposition 0 "Never Learned" 1 "Learned Party Cue" 2 "Already Knew" 3 "Forgot"
label values learn_partyposition learn_partyposition
codebook learn_partyposition

*Adding values to misssing observation regarding independent variable (learning)
bysort id (timedummy) : replace learn_partyposition = learn_partyposition[_n+1] if missing(learn_partyposition)

bro id timedummy know_own_partyposition learn_partyposition


****Issue attitude (immigration)*****

*Immigration
codebook immigSelf, tab(99)
recode immigSelf (0=0 "Allow many fewer") (1=1) (2=2) (3=3) (4=4) (5=5) (6=6) (7=7) (8=8) (9=9) (10=10 "Allow many more") (9999=.), gen(issueattitude1)
codebook immigSelf issueattitude1, tab(99)


*Creating variable that measures change in inclination to vote remain over time (used for matching analysis)
gen vote_remain_change=.
sort id timedummy
by id (timedummy), sort: replace vote_remain_change=0 if vote_remain[_n-1]==0 & (vote_remain==0)
by id (timedummy), sort: replace vote_remain_change=0 if vote_remain[_n-1]==1 & (vote_remain==1)
by id (timedummy), sort: replace vote_remain_change=1 if vote_remain[_n-1]==0 & (vote_remain==1)
by id (timedummy), sort: replace vote_remain_change=-1 if vote_remain[_n-1]==1 & (vote_remain==0)
label define vote_remain_change 0 "Did not change vote" 1 "Changed to REMAIN" -1 "Changed awaw from REMAIN"
label values vote_remain_change vote_remain_change
codebook vote_remain_change

*Adding values to misssing observation regarding independent variable (learning)
bysort id (timedummy) : replace vote_remain_change = vote_remain_change[_n+1] if missing(vote_remain_change)



**************ANALYSIS****************

*Dropping respondents who forgot policy info
drop if learn_policyinfo==3

*Figure 3
reg vote_remain i.timedummy##i.learn_policyinfo, cl( id )
margins, over( timedummy learn_policyinfo )
marginsplot, scheme(plotplain) title("               2016 UK Brexit Referendum", size(large)) subtitle({it:Policy Information: Reason for Opposing}, size(medium)) xtitle("") ytitle("Proportion Voting Remain", size(medium)) legend(off) yscale(r(0(1)1)) ylab(0(0.2)1) ylabel(0 "0%" 0.2 "20%" 0.4 "40%" 0.6 "60%" 0.8 "80%" 1 "100%", labsize(medsmall)) ciopts(recast(rspike)) xscale(r(-0.2(1.4)1.2)) xlabel(0(1)1, valuelabels nogrid labsize(medsmall)) xsize(4.5) plot1opts(mfcolor(gray)lpattern("1")msym(O)msize(medium)) plot2opts(mfcolor(black)lpattern("1")msym(O)msize(medium)) plot3opts(mfcolor(white)lpattern("1")msym(O)msize(medium)) xoverhangs text(0.1917 -0.1 "19%" 0.7406 -0.1 "74%" 0.9445 -0.1 "94%" 0.1912 1.1 "19%" 0.7925 1.1 "79%" 0.9447 1.1 "94%", size(medsmall))


*Dropping respondents who already knew policy info (not used for statistical analysis)
drop if learn_policyinfo==2


*Creating variable that measures missing values on timevarying covariates

codebook democraticsatisfaction EU_interest debateexposure know_own_partyposition issueattitude1, tab(99)

bysort id ( democraticsatisfaction ) : gen todrop1 = missing( democraticsatisfaction [1]) | missing( democraticsatisfaction [_N])
bro id timedummy democraticsatisfaction if todrop1==1

bysort id ( EU_interest ) : gen todrop2 = missing( EU_interest [1]) | missing( EU_interest [_N])
bro id timedummy EU_interest if todrop2==1

bysort id ( know_own_partyposition ) : gen todrop3 = missing( know_own_partyposition [1]) | missing( know_own_partyposition [_N])
bro id timedummy know_own_partyposition if todrop3==1

bysort id ( issueattitude1 ) : gen todrop4 = missing( issueattitude1 [1]) | missing( issueattitude1 [_N])
bro id timedummy issueattitude1 if todrop4==1

gen missing_TV_covariates=0
replace missing_TV_covariates=1 if todrop1==1 | todrop2==1 | todrop3==1 | todrop4==1


****All voters***

*Model 1
reg vote_remain i.timedummy##i.learn_policyinfo, cl(id)


*Model 2
reg vote_remain i.timedummy##i.learn_policyinfo i.democraticsatisfaction i.EU_interest debateexposure i.know_own_partyposition issueattitude1 if missing_TV_covariates==0, cl(id)

*Model 3
reg vote_remain i.timedummy##(i.learn_policyinfo i.gender i.age_cat i.education i.occupation i.region)  i.democraticsatisfaction i.EU_interest debateexposure i.know_own_partyposition issueattitude1 if missing_TV_covariates==0, cl(id)


****Voters whose party recommend voting yes (in favour of the referendum proposal)****

*Model 4
reg vote_remain i.timedummy##i.learn_policyinfo if prior_partisanship==3, cl(id)


*Model 5
reg vote_remain i.timedummy##i.learn_policyinfo i.democraticsatisfaction i.EU_interest debateexposure i.know_own_partyposition issueattitude1 if missing_TV_covariates==0 & prior_partisanship==3, cl(id)

*Model 6
reg vote_remain i.timedummy##(i.learn_policyinfo i.gender i.age_cat i.education i.occupation i.region)  i.democraticsatisfaction i.EU_interest debateexposure i.know_own_partyposition issueattitude1 if missing_TV_covariates==0 & prior_partisanship==3, cl(id)


****Matched Group****
psmatch2 learn_policyinfo i.gender i.age_cat i.education i.region i.occupation prior_ideology prior_EU_values party_like_Con party_like_Lab party_like_LD party_like_UKIP party_like_Grn if timedummy==0, out( vote_remain_change ) n(1) noreplacement
bysort id (timedummy) : replace _weight = _weight[_n-1] if missing(_weight)

*Model 7
reg vote_remain i.timedummy##i.learn_policyinfo if _weight==1, cl(id)

*Model 8
reg vote_remain i.timedummy##i.learn_policyinfo i.democraticsatisfaction i.EU_interest debateexposure i.know_own_partyposition issueattitude1  if _weight==1 & missing_TV_covariates==0, cl(id)



